<template>
  <div class="healthInfo pd-20">
    <el-button @click="handleOption('add', null)"
               type="primary">新 增</el-button>
    <el-table :data="tableData"
              border
              class="mt-20"
              style="width: 100%">
      <el-table-column align="center"
                       prop="name"
                       label="适用人群"
                       width="200"></el-table-column>
      <el-table-column align="center"
                       label="图标">
        <template slot-scope="scope">
          <img :src="scope.row.iconUrl"
               style="height:50px;width:50px">
        </template>
      </el-table-column>
      <el-table-column align="center"
                       width="180"
                       label="操作">
        <template slot-scope="scope">
          <el-button type="primary"
                     @click="handleOption('edit', scope.row)">编 辑</el-button>
          <el-button type="danger"
                     @click="handleOption('del', scope.row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <page @getList="_getList"
          :pageTotal="pageTotal"
          ref="page"></page>
    <el-dialog :title="title"
               :visible.sync="dialogVisible"
               width="30%"
               @close="dialogVisible = false"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               center>
      <el-form :model="formModel"
               label-width="100px"
               :rules="formRules"
               ref="formRefs">
        <el-form-item label="图标："
                      prop="iconUrl">
          <el-upload class="avatar-uploader"
                     :action="imageUploadUrl"
                     accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg"
                     :show-file-list="false"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="formModel.iconUrl"
                 :src="formModel.iconUrl"
                 class="avatar" />
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="适用人群："
                      prop="name">
          <el-input v-model="formModel.name"
                    maxlength="50"
                    show-word-limit
                    autocomplete="off"
                    clearable
                    placeholder="请输入适用人群"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="success"
                   @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Page from "@/base/page/page";
  import { mapGetters } from "vuex";

  export default {
    name: "one", // 适用人群
    data () {
      return {
        title: '新增适用人群',
        formRules: {
          iconUrl: [
            { required: true, message: '请上传图标', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入适用人群', trigger: 'blur' }
          ],
        },
        formModel: {
          iconUrl: null,
          name: ''
        },
        imageUploadUrl: this.api.appUpgradeUpload.url,
        dialogVisible: false,
        param: {},
        tableData: [],
        pageTotal: 0
      };
    },
    created () {
      this._getList();
    },
    methods: {
      handleOption (op, row) {
        switch (op) {
          case 'add':
            this.dialogVisible = true;
            this.title = '新增适用人群';
            this.$nextTick(() => {
              this.$refs.formRefs.resetFields();
            })
            break;
          case 'edit':
            this.dialogVisible = true;
            this.title = '编辑适用人群';
            this.formModel = Object.assign({}, row);
            break;
          case 'del':
            this.$confirm('此操作将永久删除, 是否继续？')
              .then(_ => {
                this.post(this.api.cooperationHospital.deleteApplicableThrong, { id: row.id }).then(res => {
                  if (res.data.code === '200') {
                    this.$message.success('删除成功!');
                    this._getList();
                  }
                })
              })
              .catch(_ => { });
            break;
        }
      },
      submitForm () {
        this.$refs.formRefs.validate((valid) => {
          if (valid) {
            this.post(this.api.cooperationHospital.uploadApplicableThrong, this.formModel).then(res => {
              if (res.data.code === '200') {
                this.dialogVisible = false;
                this.$message.success("保存成功");
                this._getList();
              }
            });
          }
        })
      },

      _getList () {
        this.param.pageIndex = this.page.pageIndex;
        this.param.pageSize = this.page.pageSize;

        this.post(this.api.cooperationHospital.getApplicableThrongPage, this.param).then(res => {
          if (res.data.code === '200') {
            this.tableData = res.data.object;
            this.pageTotal = res.data.totalNum;
          }
        });
      },
      handleAvatarSuccess (res) {
        this.formModel.iconUrl = res.object[0];
      },
      beforeAvatarUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error("上传图片大小不能超过 2MB!");
        }
        return isLt2M;
      }
    },
    computed: {
      ...mapGetters(["page"])
    },
    components: {
      Page
    }
  };
</script>

<style scoped lang="scss" >
  .avatar-uploader {
    width: 100px;
    height: 100px;
    border: 1px dashed #606266;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader:hover {
    border-color: #409eff;
  }
  .avatar-uploader .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 98px;
    height: 98px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
